// order/order.js
var util = require('../utils/util.js')
const app = getApp();

Page({
    data: {
        table_active: 0,
        orderList: null,
        currentOrderList: null,
        orderMap: null,//用来支付
        page: 1,
        limit: 100,
        hasNext:true
    },
    onLoad(e) {
        this.getUserOrderList();
    },
    removeDataOrder(idx,id){
        let orderList = this.data.orderList;
        orderList.splice(idx,1);
        let currentOrderList = this.data.currentOrderList;
        currentOrderList.splice(idx, 1);
        let orderMap = this.data.orderMap;
        orderMap.delete(id);
        this.setData({
            orderList: orderList,
            currentOrderList: currentOrderList,
            orderMap: orderMap
        })
    },
    getUserOrderList() {
        let url = "/order/washCarOrder/findWashCarOrderList";
        let userId = app.globalData.user.id;
        let that = this;
        //服务信息map用来获取服务名称
        var serverInfoMap = app.globalData.serverInfoMap;
        // console.log(serverInfoMap)
        //订单map,可以根据id快速获取订单
        let page = that.data.page
        if (page == 1){
            that.data.orderMap = new Map();
        }

        app.getJson(url, {
            userId: userId,
            page: page,
            limit: that.data.limit
        }, res => {
            if (res.code == 0) {
                if (res.current == res.pages){
                    that.setData({
                        hasNext:false
                    })
                }
                for (let i in res.data) {
                    let serverInfo = serverInfoMap.get(res.data[i].serverType);
                    res.data[i].serverName = serverInfo.serverName + " (" + serverInfo.remakes + ")"
                    res.data[i].washStartTime = util.formatDate(res.data[i].washStartTime, "yyyy/MM/dd HH:mm");
                    res.data[i].washEndTime = util.formatDate(res.data[i].washEndTime, "yyyy/MM/dd HH:mm");
                    that.data.orderMap.set(res.data[i].id, res.data[i]);
                }
                
                if (page == 1){
                    that.setData({
                        orderList: res.data,
                    })
                }else if (page>1 ){
                    let orderList = that.data.orderList;
                    for (let i in res.data) {
                        orderList.push(res.data[i]);
                    }
                    that.setData({
                        orderList: orderList,
                    })
                }
                // util.log(res)
                that.setCurrentOrderList(that.data.orderList, util.ALL_ORDER)
            }

        });
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function (e) {
        console.log(e);
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function (e) {
        if(this.data.hasNext){
            this.setData({
                page: ++this.data.page
            })
            this.getUserOrderList();
        }
    },
    operatorOrder(e) {
        let method = e.target.dataset.method;
        let dataset = e.currentTarget.dataset;
        console.log(method, dataset);
        // return;
        if ("payOrder" == method) {
            this.payOrder(dataset);
        } else if ("cancelOrder" == method) {
            this.cancelOrder(dataset);
        } else if ("deleteOrder" == method) {
            this.setData({
                showModal: true
            })
            this.setData({
                delDataset: dataset
            })
            return;
            
        }
    },
    payOrder(dataset) {
        let id = dataset.id;
        let idx = dataset.index;
        let order = this.data.orderMap.get(id);
        let payData = JSON.parse(order.payData);
        delete payData.appId;
        console.log(payData)
        wx.requestPayment(payData)
    },
    cancelOrder(dataset) {
        let id = dataset.id;
        let idx = dataset.index;
        let url = "/order/washCarOrder/cancelWashCarOrder";
        let that = this;
        app.getJson(url, {
            id: id
        }, res => {
            if (res.code == 0) {
                that.getUserOrderList()
                wx.showToast({
                    title: '取消成功',
                })
            } else {
                wx.showModal({
                    title: '取消失败, ',
                    content: '服务器繁忙, 请稍后再试.',
                    showCancel: false
                })
            }
        });
    },
    selectDeleteOrder(e){
        let method = e.target.dataset.method;
        let dataset = this.data.delDataset;
        if (method == 'okDel'){
            this.deleteOrder(dataset);
        } else if (method != 'cancelDel'){
            return;
        }
        this.setData({
            delDataset: null,
            showModal: false
        })
        return;
    },
    deleteOrder(dataset) {
        let id = dataset.id;
        let idx = dataset.index;
        let url = "/order/washCarOrder/deleteWashCarOrder";
        let that = this;
        app.getJson(url, {
            id: id
        }, res => {
            if (res.code == 0) {
                that.removeDataOrder(idx, id)
                wx.showToast({
                    title: '删除成功',
                })
            } else {
                wx.showModal({
                    title: '删除失败, ',
                    content: '服务器繁忙, 请稍后再试.',
                    showCancel: false
                })
            }
        });
    },
    /** 
     * {} 全部
      {status=4,serviceStatus=1} 待服务
      {status=4,serviceStatus=2} 服务中
      {status=4,serviceStatus=3} 已完成
      {status=6} 已撤销 */
    setCurrentOrderList(list, t) {
        let cList = [];
        let status, serviceStatus;
        let stateName;
        util.log(t);
        if (t == 0) {
            this.setData({
                currentOrderList: list
            })
            return;
        } else if (t == 1) {
            status = 4;
            serviceStatus = 1;
            stateName = "待服务";
        } else if (t == 2) {
            status = 4;
            serviceStatus = 2;
            stateName = "服务中";
        } else if (t == 3) {
            status = 4;
            serviceStatus = 3;
            stateName = "已完成";
        } else if (t == 4) {
            status = 6;
            stateName = "已撤销";
        }
        for (var i in list) {
            if (status && serviceStatus){
                if ( status == list[i].status && serviceStatus == list[i].serviceStatus){
                    list[i].stateName = stateName;
                    cList.push(list[i]);
                }
            } else if (status && !serviceStatus){
                if (status == list[i].status) {
                    list[i].stateName = stateName;
                    cList.push(list[i]);
                }
            }
        }
        util.log(cList);
        this.setData({
            currentOrderList: cList
        })
    },
    onAll(e) {
        this.setData({
            table_active: 0
        })
        this.setCurrentOrderList(this.data.orderList, 0);
    },
    onWait(e) {
        this.setData({
            table_active: 1
        })
        this.setCurrentOrderList(this.data.orderList, 1);
    },
    onInServer(e) {
        this.setData({
            table_active: 2
        })
        this.setCurrentOrderList(this.data.orderList, 2);
    },
    onComplete(e) {
        this.setData({
            table_active: 3
        })
        this.setCurrentOrderList(this.data.orderList, 3);
    },
    onCancel(e) {
        this.setData({
            table_active: 4
        })
        this.setCurrentOrderList(this.data.orderList, 4);
    },
    // wx.navigate(e){
    //   wx.redirectTo(OBJECT)
    // }

})